//管理员模块
//引入express模块
const express = require("express");
//引入pool连接池模块
const pool =require("../pool");
//创建路由对象
const router = express.Router();
//所有路由
//*************预留路由位置 ↓↓↓↓↓************
//1.首页渲染热门城市
//接口地址：http://127.0.0.1:9000/admin/hcity
//请求方式：get
router.get("/hcity",(req,res,next)=>{
  //选择热度排行前4的四个城市
  let sql="select d_number,d_id,d_name,d_pic,d_ptit from yms_address order by d_heat desc limit 0,4;"
  pool.query(sql,(err,result)=>{
    if (err) { return next(err)}
    //console.log(result);
    res.send({
      code:200,
      msg:"热度排行前4的城市",
      data:result
    })
  });
});
//2.首页加载时查询所有城市名，唯一值
//接口地址：http://127.0.0.1:9000/admin/cityname
//请求方式：get
router.get("/cityname",(req,res,next)=>{
  //选择城市唯一值
  let sql="select distinct d_name from yms_address;"
  pool.query(sql,(err,result)=>{
    if (err) { return next(err)}
    //console.log(result);
    res.send({
      code:200,
      msg:"查询成功",
      data:result
    })
  });
});

//3.首页根据传入的d_name查询某个房间具体信息
//接口地址：http://127.0.0.1:9000/admin/roomlist/s_id=？
//请求方式：get


//4.首页渲染热门故事
//接口地址：http://127.0.0.1:9000/admin/hstory
//请求方式：get
router.get("/hstory",(req,res,next)=>{
  //选择热度排行前4的四个故事
  let sql="select s_id,s_pic,s_tit,s_text from yms_story order by s_heat desc limit 0,4;"
  pool.query(sql,(err,result)=>{
    if (err) { return next(err)}
    //console.log(result);
    res.send({
      code:200,
      msg:"热度排行前4的故事",
      data:result
    })
  });
});
//5.根据传入的id查询某个故事
//接口地址：http://127.0.0.1:9000/admin/storylist/s_id=？
//请求方式：get
  router.get("/storylist/:s_id",(req,res,next)=>{
    let obj = req.params;
    //console.log(obj);
    let sql = "select * from yms_story where s_id=?;";
    pool.query(sql,[obj.s_id],(err,result)=>{
      if (err) { return next(err)}
      //console.log(result);
      if (result.length){
        res.send({
          code:200,
          msg:"查询成功",
          data:result
        })
      }else{
        res.send({
          code:201,
          msg:"查询失败",
        })
      }
    })
  });
//6.分页查询所有的故事，每页6条数据，第一页 0-6，第二页 6-12...
//接口地址：http://127.0.0.1:9000/admin/story
//请求方式：get
  router.get("/story/:page",(req,res,next)=>{
    let obj = req.params;
    //console.log(obj);
    let sql = "select * from yms_story where s_id != 0 limit ?,6;";
    pool.query(sql,[(obj.page-1)*6],(err,result)=>{
      if (err) { return next(err)}
      //console.log(result);
      if (result.length){
        res.send({
          code:200,
          msg:"查询成功",
          data:result
        })
      }else{
        res.send({
          code:201,
          msg:"查询失败",
        })
      }
    })
  });

//7.搜索页根据传入的r_id查询某个房间具体信息
//接口地址：http://127.0.0.1:9000/admin/roomdetail/r_id=？
//请求方式：get

router.get("/roomdetail/:r_id",(req,res,next)=>{
  let obj = req.params;
  //console.log(obj);
  let sql = "select * from yms_room where r_id=?;";
  pool.query(sql,[obj.r_id],(err,result)=>{
    if (err) { return next(err)}
    //console.log(result);
    if (result.length){
      res.send({
        code:200,
        msg:"查询成功",
        data:result
      })
    }else{
      res.send({
        code:201,
        msg:"查询失败",
      })
    }
  })
});

//8.房间具体信息页面根据传入的r_id查询房间的评论信息
//接口地址：http://127.0.0.1:9000/admin/roomtalk/r_id=？
//请求方式：get
router.get("/roomtalk/:r_id",(req,res,next)=>{
  let obj = req.params;
  //console.log(obj);
  let sql = "select * from yms_talk where r_id=?;";
  pool.query(sql,[obj.r_id],(err,result)=>{
    if (err) { return next(err)}
    //console.log(result);
    if (result.length){
      res.send({
        code:200,
        msg:"查询成功",
        data:result
      })
    }else{
      res.send({
        code:201,
        msg:"查询失败",
      })
    }
  })
});

//*************预留路由位置 ↑↑↑↑↑↑************
//导出路由对象
module.exports = router;